BUI.use(['bui/overlay','bui/slider','bui/menu','bui/calendar','bui/tooltip'],function(Overlay,Slider,Menu,Calendar,Tooltip){
	var honorMenu = new Menu.PopMenu({
		trigger:".js-honor",
		autoRender:true,
		children:[{id:"ch",content:"荣誉称号"},{id:"ch-ph",content:" --称号排行"},{id:"jz",content:"荣誉奖章"},{id:"jz-ph",content:" --奖章排行"},]
	});
	honorMenu.on('itemselected', function(){
		var id = honorMenu.getSelectedValue();
		switch(id){
			case "ch":
			location.href="/?m=Kxdr&c=Honor&a=index";break;
			case "jz":
			location.href="/?m=Kxdr&c=Medal";break;
		}
	});
	 var dropMenu1 = new Menu.PopMenu({
          trigger : '#btn',
          autoRender : true,
          width : 140,
          children : [
			// {id:'business',content : "商务版"},
			{id:"sports",content : "体育版"},
			// {id:"tvshow",content : "电视版"},
			// {id:"education",content : "教育版"},
			// {id:"since",content : "科技版"},
			// {id:"fashion",content : "时尚版"},
			// {id:"change_event",content : "切换活动类型"}
			]
        });
        dropMenu1.on('itemselected', function(){
			var version = dropMenu1.getSelectedValue();
			if(version=="change_event"){
				location.href="/?a=gamecfg&m=shopadmin&repick=1";
				return;
			}
          // alert(dropMenu1.getSelectedText() + '：' + dropMenu1.getSelectedValue());
			$.ajax(
			{"url":"/?m=Kxdr&c=Gameset&a=set_app_version",
				"data":{"version":version},				  
				"dataType": "json",
				"success":function(data,status){
					if(data.hasError){
						showDialog(data.error);
					}else{
						$(".js-version").html(dropMenu1.getSelectedText());
						window.location.href = "/?m=Kxdr&c=Index&a=index&ver="+version;
					}
				}
			});
        });
	var html = "";
			html += ("<div class=\"tips tips-large tips-success\">");
			html += ("          <span class=\"x-icon x-icon-success\"><i class=\"icon icon-ok icon-white\"></i></span>");
			html += ("          <div class=\"tips-content\">");
			html += ("            <h2>欢迎进入狂想达人！</h2>");
			html += ("            <p class=\"auxiliary-text\">");
			html += ("              您也可以暂不启用，等设置完毕后再启用不迟：");
			html += ("            </p>");
			html += ("            <p>");
			html += ("              <h4>确认启用请点击确定，店铺插件会立即变更为狂想模式。</h4>");
			html += ("            </p>");
			html += ("          </div>");
			html += ("        </div>");
          var dialog = new Overlay.Dialog({
            title:'请确认启用',
            width:500,
            height:210,
            mask:true,
            buttons:[{
                text:'确认启用',
                elCls : 'button button-primary',
                handler : function(){
					$.ajax(
						{"url":"/?m=Kxdr&c=Gameset&a=setcur",			  
							"dataType": "json",
							"success":function(data,status){
								if(data.hasError){
									alert("设置出错！"+data.error);
								}else{
									location.href="/?m=Kxdr&c=Index&a=index";
								}
							}
						});
                }
              },{
                text:'暂不启用',
                elCls : 'button button-mini',
                handler : function(){
                  //do some thing
                  this.close();
                }
              }],
            bodyContent:html
          });
		  // if($(".curtool").val()!="kxdr"){
			// dialog.show();
		  // }
		  $(".js-queren").on("click",function(){
			dialog.show();
		  });
$(".js-event").on("click",function(){
	var eid = $(this).attr("data-eid");
	var html = "<div class=\"panel-body js-content-"+eid+"\">";
		html +="<p>这里是面板内容</p>";
		html +="</div>";
	if($(".js-icon-"+eid).hasClass("icon-chevron-up")){
		$(".js-content-"+eid).animate({height:'show'},false,false,function(){liuxiaofan(eid)});
		// $(".js-content-"+eid).show();
		$(".js-icon-"+eid).addClass("icon-chevron-down");
		$(".js-icon-"+eid).removeClass("icon-chevron-up");
	}else{
		$(".js-content-"+eid).animate({height:'hide'});
		// $(".js-content-"+eid).hide();
		$(".js-icon-"+eid).addClass("icon-chevron-up");
		$(".js-icon-"+eid).removeClass("icon-chevron-down");
	}
});
$("h1,h2,h3,h4,.title,.gift_hqfs,.players").on("hover",function(){
	if($(this).children("button").hasClass("hide")){
		$(this).children("button").removeClass("hide");
	}else{
		$(this).children("button").addClass("hide");
	}	
	if($(this).children("a").hasClass("hide")){
		$(this).children("a").removeClass("hide");
	}else{
		$(this).children("a").addClass("hide");
	}
});
	  // 拖动选择奖品数量的控件
	  var slider1 = new Slider.Slider({
        render : '#sl2',
        max : 1, //最大值
        value : 0 //当前值
      });
      slider1.render();
      slider1.on('change',function(ev){
        $('.js-jpsl').val(ev.value);
      });
$(".js-edit").on("click",function(){
	var field = $(this).attr("data-field");
	var id = $(this).attr("data-eid");
	var what = $(this).attr("data-gift")?"_"+$(this).attr("data-gift"):id;
	var base_value = $(this).attr("data-value");
	var html = "<center><input type=\"text\" class=\"val\" value=\""+base_value+"\" /></center>";
	if(field=="think_type"){
		var way = $(this).attr("data-way");
		html+="<p><center><select class=\"think_type_way\">";
		html+="<option value=\"1\""+(way==1?"selected=\"selected\"":"")+">只猜“是”</option>";
		html+="<option value=\"2\""+(way==2?"selected=\"selected\"":"")+">只猜“否”</option>";
		html+="<option value=\"3\""+(way==3?"selected=\"selected\"":"")+">猜“是”、“否”</option>";
		html+="<option value=\"0\""+(way==0?"selected=\"selected\"":"")+">猜数据</option>";
		html+="</select></center></p>"
	}
	if(field=="gift_event_quant"){
		html=false;
		var content_id = "content";
	}
		  var dlg = new Overlay.Dialog({
			"width":500,
            "title":"修改",
            //配置DOM容器的编号
			"mask":true,
            "buttons":[{
						text:'修改',
						elCls : 'button button-danger',
						handler : function(){
							var value = $(".val").val();
							var way = $(".think_type_way").val();
							var way_text = $(".think_type_way option:selected").text();
							if(!value){alert("请输入数据！");return;}
						  		$.ajax(
								{"url":"/?m=Kxdr&c=Gameset&a=save_edit",
									"data":{"id":id,"field":field,"value":value,"way":way},				  
									"dataType": "json",
									"success":function(data,status){
										if(data.hasError){
											alert("编辑出错！"+data.error);
										}else{
											$("."+field+what).html(value);
											if(way){
											$(".think_type_way"+what).html(way_text);
											}
											dlg.destroy();
										}
									}
								});
						}
					  },{
						text:'关闭',
						elCls : 'button',
						handler : function(){
						  this.destroy();
						}
					  }],
			"contentId":content_id,
			"bodyContent":html
          });
		  dlg.show();
});
	function edit_gift(html){
		var dialoggft = new Overlay.Dialog({
			title:'添加推荐商品',
			width:930,
			height:600,
			//配置DOM容器的编号
			bodyContent:html,
		});
		dialoggft.show();
	}		
          var dialog = new Overlay.Dialog({
            title:'非模态窗口',
            width:330,
            height:200,
            mask:false,
            buttons:[{
                text:'确定',
                elCls : 'button pull-left offset3',
                handler : function(){
					$(".js-fbst-show").text($(".js-fbst").val());
					$(".js-fbed-show").text($(".js-fbed").val());
                  this.close();
                }
              }],
            contentId:'content',
          });
		  var datepicker = new Calendar.DatePicker({
            trigger:'.calendar',
            autoRender : true
          });
        $('.js-showcal').on('click',function () {
          dialog.show();
        });
		$(".remove_event").on("click",function(){
			var eid = $(this).attr("data-eid");
			var dialoggft = new Overlay.Dialog({
					title:'确认删除',
					mask:false,
					//配置DOM容器的编号
					"buttons":[{
						text:'确定',
						elCls : 'button button-danger',
						handler : function(){
							this.destroy();
							$.ajax(
								{"url":"/?m=Kxdr&c=event&a=remove_event",
									"data":{"eid":eid},				  
									"dataType": "json",
									"success":function(data,status){
										 BUI.Message.Alert(data.msg,function(){location.reload();},'info');
									}
								});
						}
					},{
						text:'取消',
						elCls : 'button',
						handler : function(){
							this.destroy();
						}
					}
					],
					bodyContent:"您确定删除这个活动吗？删除活动并不会删除比赛，您可以重新选择比赛创建活动。",
			}).show();;
		});
      });
/*
瀑布显示猜奖品

原理：1.把所有的li的高度值放到数组里面
     2.第一行的top都为0
     3.计算高度值最小的值是哪个li
     4.把接下来的li放到那个li的下面
*/

function liuxiaofan(eid){
	var isok = $(".giftbox"+eid).attr("data-isok");
	if(isok){
		return;
	}
	var margin = 60;//设置间距
	var li=$(".giftshow"+eid);//区块名称
	if(li.length<=0){
		return;
	}
	var li_W = li[0].offsetWidth+margin;//取区块的实际宽度
    var h=[];//记录区块高度的数组
    var n = 960/li_W|0;
    for(var i = 0;i < li.length;i++) {
        li_H = li[i].offsetHeight;//获取每个li的高度
        if(i < n) {//n是一行最多的li，所以小于n就是第一行了
            max_H =Math.max.apply(null,h);
            h[i]=li_H;//把每个li放到数组里面
            li.eq(i).css("top",10);//第一行的Li的top值为0
            li.eq(i).css("left",i * li_W);//第i个li的左坐标就是i*li的宽度
            }
        else{
            min_H =Math.min.apply(null,h) ;//取得数组中的最小值，区块中高度值最小的那个
            minKey = getarraykey(h, min_H);//最小的值对应的指针
            h[minKey] += li_H+margin ;//加上新高度后更新高度值
            li.eq(i).css("top",min_H+margin);//先得到高度最小的Li，然后把接下来的li放到它的下面
            li.eq(i).css("left",minKey * li_W);    //第i个li的左坐标就是i*li的宽度
        }
      //  $("p").eq(i).text("高度："+li_H);//把区块高度值写入对应的区块H2标题里面
    }
    max =Math.max.apply(null,h) ;
    $(".giftbox"+eid).css("height",max);
    $(".giftbox"+eid).attr("data-isok",true);
}
/* 使用for in运算返回数组中某一值的对应项数(比如算出最小的高度值是数组里面的第几个) */
function getarraykey(s, v) {for(k in s) {if(s[k] == v) {return k;}}}
/*这里一定要用onload，因为图片不加载完就不知道高度值*/
// window.onload = function() {liuxiaofan();};
// window.onresize = function() {liuxiaofan();};
